package com.example.remoteservice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private IMyAidlInterface iRemoteAidlInterface;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iRemoteAidlInterface = IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private ILocalAidlInterface iLocalAidlInterface;
    private ServiceConnection localConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iLocalAidlInterface = ILocalAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private Messenger messenger;
    private ServiceConnection messengerConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            messenger = new Messenger(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("111111", " remoteservice MainActivity onCreate");
        setContentView(R.layout.activity_main);
//1、启动本地服务供其他应用的进程调用，实现AIDL接口，适用于远端其他应用多线程调用
        // 当前的remoteservice这个应用是服务端，RemoteService是和MainActivity一个进程的，
        // RemoteService这个服务是启动后给com.example.zz.example.mediaplayer.ServiceActivity绑定进行跨进程使用aidl通信的，
        // 是实现不同应用的不同进程间的通信
        // 通过bindService启动本地的RemoteService，启动完成后获得iRemoteAidlInterface，
        // 可以通过iRemoteAidlInterface给远端绑定的应用发送跨进程信息，
        // 这里的案例没有向远端发送消息，只在服务端接收了消息，消息由远端ServiceActivity进行绑定发送过来
        Intent intent = new Intent(this, RemoteService.class);
        bindService(intent, conn, BIND_AUTO_CREATE);

//2、启动本地不同进程服务供本应用内跨进程调用
        // LocalService是和MainActivity不同进程的，LocalService在manifet中单独设置了进程，
        // 可以实现同应用的不同进程间的通信
        Intent localIntent = new Intent(this, LocalService.class);
        bindService(localIntent, localConn, BIND_AUTO_CREATE);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    iLocalAidlInterface.basicTypes(0,0L,true,0f,0d,"从主进程进行点击，把这条信息通过aidl通信传给服务进程");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

//1、启动本地服务供其他应用的进程调用，实现Messenger接口，适用于远端其他应用单线程调用
        Intent messengerIntent = new Intent(this, MessengerTestService.class);
        bindService(messengerIntent, messengerConn, BIND_AUTO_CREATE);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}
